package main;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import tools.Help;
import tools.Log;
import tools.Point;

import graphics.system.Resolution;
public final class Config {
	public static final boolean NOPLAYER = false;
	/**
	 * 1 = best quality >1 = lower quality and some effects are disabled at all(=much faster rendering)
	 */
	public static int SHADER_QUALITY = 1;
	public static int MINFPS = 10;
	public static int MAXFPS = 30;
	public static int PHYSICSSPEED = 2;
	public static String SCREENSHOTFOLDER = "../screenshots/";
	public static String MODELFOLDER = "resources/models/";
	public static Point RESOLUTION = new Point(800, 600);
	public static float VOLUME = 1;
	private static void processConfigLine(String identifier, String value) {
		if ("quality".equals(identifier)) {
			SHADER_QUALITY = Integer.valueOf(value);
		}
		else if ("maxfps".equals(identifier)) {
			MAXFPS = Integer.valueOf(value);
		}
		else if ("screenshotfolder".equals(identifier)) {
			SCREENSHOTFOLDER = value;
		}
		else if ("physicsspeed".equals(identifier)) {
			PHYSICSSPEED = Integer.valueOf(value);
		}
		else if ("modelfolder".equals(identifier)) {
			MODELFOLDER = value;
		}
		else if ("screen".equals(identifier)) {
			RESOLUTION = readPoint(value);
		}
		else if ("volume".equals(identifier)) {
			VOLUME = Float.valueOf(value);
		}
	}
	public static void load(String fname) {
		FileInputStream f;
		try {
			f = new FileInputStream(fname);
			DataInputStream in = new DataInputStream(f);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String c = "";
			String line = "";
			while ((line = br.readLine()) != null) {
				c = c + line;
			}
			String l[] = c.split(";");
			for (String s : l) {
				String ll[] = s.replaceAll(" ", "").split("=");
				String identifier = ll[0].toLowerCase();
				String value = ll[1];
				processConfigLine(identifier, value);
			}
		} catch (FileNotFoundException e) {
			Help.p("Error: No such config file:" + fname);
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private static Point readPoint(String value) {
		String p[] = value.split(",");
		int w = Integer.valueOf(p[0]);
		int h = Integer.valueOf(p[1]);
		return new Point((float) w, (float) h);
	}
}
